//
//  LogoInViewController.m
//  WhatToEat
//
//  Created by Mac on 15/3/18.
//  Copyright (c) 2015年 Mac. All rights reserved.
//

#import "LogoInViewController.h"
#import "CompleteTableController.h"
#import "FLEXManager.h"
#import "EARegisterViewController.h"
#import "EAChooseMatch.h"
#import "EAFoodAndWaterController.h"

@interface LogoInViewController ()

@end

@implementation LogoInViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    [self updateUI];
}


-(void)updateUI
{


    
    UIBarButtonItem *bt=[[UIBarButtonItem alloc] init];
    bt.title=@"返回";
    self.navigationItem.backBarButtonItem=bt;

    self.logInBtn.layer.cornerRadius=kRadius;
    self.logInBtn.clipsToBounds=YES;

    self.logInBtn.backgroundColor=kTingColor;
    
    
    self.resigBtn.layer.borderColor=[UIColor lightGrayColor].CGColor;
    self.resigBtn.layer.cornerRadius=kRadius;
    self.resigBtn.backgroundColor=kWhiteColor;
    self.resigBtn.layer.borderWidth=1.3;
    [self.resigBtn setTitleColor:[UIColor lightGrayColor] forState:0];


    self.navigationItem.leftBarButtonItem=nil;
    
    

    
}



- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)strin
{
    if ([strin isEqualToString:@"\n"]&&textField.tag==1) {
        [self loginAction:nil];
    }
    
    

    return YES;
}






- (IBAction)loginAction:(id)sender {
    
    
    
    if (self.userNameTF.text.length==0) {
        [SVProgressHUD showImage:nil status:@"请输入用户名"];
        return;
    }
    
    if (self.passWardTF.text.length==0) {
        [SVProgressHUD showImage:nil status:@"请输入密码"];
        return;
    }
    
    
    [WebAPI webAPISignAddMd5tWithNote:@"login/login" parm:@{@"username":self.userNameTF.text,
                                                            @"password":self.passWardTF.text
                                                            
                                                            
                                                            } completed:^(NSInteger state, AFHTTPRequestOperation *operation, id responseObject) {
                                                                
                                                                
        

        if ([responseObject[@"code"] isEqualToString:@"1"]) {
            
            
            
            [EASaveUserMessage saveUserMessage:responseObject andView:nil];
            
            
            
            if ([responseObject[@"user"][@"new"] isEqualToString:@"1"]) {
                
                
                
                CompleteTableController * cVC=[self.storyboard instantiateViewControllerWithIdentifier:@"CompleteTableController"];
                
                
                [self.navigationController pushViewController:cVC animated:YES];
                
                
                
                
                
            }else
            {
            
                
                UIViewController * vc=[ApplicationDelegate.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"MainViewControllerNV"];
                
                
                [self presentViewController:vc animated:YES completion:^{
                    
                }];
            
            
            
            }
            
            
            
            
        }
        

                                                                if ([kStr(responseObject[@"code"]) isEqualToString:@"9998"]) {
                                                                    
                                                                    
                                                                    [SVProgressHUD showImage:nil status:@"用户名或密码错误"];
                                                                    
                                                                    self.userNameTF.text=@"";
                                                                    self.passWardTF.text=@"";
                                                                    
                                                                    [self.userNameTF resignFirstResponder];
                                                                    
                                                                    
                                                                    
                                                                }
        
        
        
        
    }];
    
    



}


-(void)saveUserMessage:(NSDictionary *)responseObject
{

    NSString *msg=[NSString stringWithFormat:@"%@",responseObject[@"code"]];
    
    if ([msg isEqualToString:@"1"]) {
        
        
        ApplicationDelegate.userId=responseObject[@"data"][@"user"][@"id"];
        ApplicationDelegate.useName=responseObject[@"data"][@"user"][@"username"];
        ApplicationDelegate.headImage=responseObject[@"data"][@"user"][@"avatar"];
        
        
        
        
        

        
        
    }
    
    
    

}




- (IBAction)thridLogIn:(UIButton *)sender
{
    
    
    
    switch (sender.tag) {
        case 0:
    
            {
                
                
                UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
                
                snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response)
                                              {
                                                  
                                                  
                                                  
                                                  NSLog(@"response is %@",response);
                                                  if (response&&[response.message isEqualToString:@"no error"]) {
                                                      
                                                      
                                                      
                                                      dispatch_async(dispatch_get_main_queue(), ^{
                                                          
                                                          
                                                          
                                                          
                                                          
                                                          
                                                          
                                                          
                                                          
                                                          [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToQQ  completion:^(UMSocialResponseEntity *response){
                                                              
                                                              
                                                              
                                                              NSLog(@"SnsInformation is %@",response.data);
                                                              
                                                              NSDictionary *dic=response.data;
                                                              
                                                              
                                                              
                                                              NSDictionary *dict =@{
                                                                                    @"type":@"weibo",
                                                                                    
                                                                                    @"id":dic[@"uid"],
                                                                                    @"username":dic[@"screen_name"],
                                                                                    @"image":dic[@"profile_image_url"]
                                                                                    
                                                                                    
                                                                                    };
                                                              
                                                              
                                                              
                                                              
                                                              
                                                              
                                                              
                                                              
                                                              [self thirdLoginWithUser:dict];
                                                              
                                                              
                                                              
                                                              
                                                              
                                                              
                                                          }];
                                                          
                                                          
                                                          
                                                          
                                                      });
                                                      
                                                      
                                                      
                                                      
                                                      
                                                      
                                                      
                                                      
                                                      
                                                  }
                                                  
                                                  
                                              });
                
                
                
                
            }
        
        
        

            
            
            
            break;
        case 1:
        {
            

            UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
            
            snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
                
              if (response.responseCode == UMSResponseCodeSuccess) {

                
                    
                    
        [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToWechatSession  completion:^(UMSocialResponseEntity *response){
                        
           NSLog(@"SnsInformation is %@",response.data);
            
            
            if (response.responseCode==UMSResponseCodeSuccess) {
                
                NSDictionary *dic=response.data;
                
                NSDictionary *dict =@{
                                      @"type":@"wx",
                                      
                                      @"id":dic[@"openid"],
                                      @"username":dic[@"screen_name"],
                                      @"image":dic[@"profile_image_url"]
                                      
                                      
                                      };
                

                
                
                
                [self thirdLoginWithUser:dict];
                
                
                
            }
    
            
            

            
            
            
            
            
            
            
            
                    }];
                
                  
                  
             }
                
            });
            
            
        
        }
            break;
        case 2:
            
        {

            UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
            snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response)
                                          {
                                              
                                              
                                              
                                              NSLog(@"response is %@",response);
                                              if (response&&[response.message isEqualToString:@"no error"]) {
                                                  
                                                  
                                                  [UMSocialConfig setFollowWeiboUids:@{UMShareToSina:@"孩教圈"}];
                                                  
                                                  dispatch_async(dispatch_get_main_queue(), ^{
                                                      [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToSina  completion:^(UMSocialResponseEntity *response){
                                                          
                                                          
                                                          
                                                          NSLog(@"SnsInformation is %@",response.data);
                                                          
                                                          
                                                          
                                                          
                                                          if (response&&[response.message isEqualToString:@"no error"])
                                                          {
                                                              
                                                              NSDictionary *dic=response.data;
                                                              
                                                              
                                                              
                                                              
                                                              NSDictionary *dict =@{
                                                                                    @"type":@"weibo",
                                                                                    
                                                                                    @"id":dic[@"uid"],
                                                                                    @"username":dic[@"screen_name"],
                                                                                    @"image":dic[@"profile_image_url"]
                                                                                    
                                                                                    
                                                                                    };
                                                              
                                                       
                                                              
                                                              
                                                              
                                                              
                                                              [self thirdLoginWithUser:dict];
                                                  
                                                              
                                                              
                                                              
                                                              
                                                              
                                                              
                                                              
                                                          }
                                                          
                                                          
                                                          
                                                          
                                                      }];
                                                      
                                                      
                                                      
                                                  });
                                                  
                                              }
                                              
                                          });

            
            
            
            
        
        
        }
            
            break;
            
        default:
            break;
    }
    
    
    
    
    
    
    
    
    
    
    
}


#pragma mark---第三方请求
-(void)thirdLoginWithUser:(NSDictionary * )d
{
[WebAPI webAPISignAddMd5tWithNote:@"login/wwq" parm:d completed:^(NSInteger state, AFHTTPRequestOperation *operation, id responseObject) {
    
    

    NSString *codeStr=[NSString stringWithFormat:@"%@",responseObject[@"code"]];

    
    if ([codeStr isEqualToString:@"1005"]) {
        
        CompleteTableController *cVC=[self.storyboard instantiateViewControllerWithIdentifier:@"CompleteTableController"];
        
        cVC.isInformation=NO;
        
        [self.navigationController pushViewController:cVC animated:YES];

        
    }
    
    
    if ([codeStr isEqualToString:@"1"]) {
        

        
        [EASaveUserMessage saveUserMessage:responseObject andView:self.view];
        
    }
    

    
    
}];
    
    
    



}









- (IBAction)btActions:(UIButton *)sender
{
    
    
    
    
    switch (sender.tag) {
        case 0:
        {
            
            
            
            

            
            
            
            
            EARegisterViewController *eVC=[self.storyboard instantiateViewControllerWithIdentifier:@"EARegisterViewController"];
            [self.navigationController pushViewController:eVC animated:YES];
        
        
        
        }
            
            
            
            break;
            
        case 1:
        {
            EAChooseMatch *Evc=[self.storyboard instantiateViewControllerWithIdentifier:@"EASearchPassword"];
            
            [self.navigationController pushViewController:Evc animated:YES];
            
            
        }
            break;
            
        default:
            break;
    }
    
}





@end
